package com.fionera.sample.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fionera.sample.BuildConfig;
import com.fionera.sample.R;
import com.fionera.sample.util.ShowToast;
import com.wsdk.service.BluetoothLeService;
import com.wsdk.util.StampFullUtil;
import com.wsdk.util.StampUtil;

import java.util.Locale;

/**
 * @author fionera
 */
public class StampActivity
        extends TitleBarActivity
            implements StampUtil.StampCallback {

    private ImageView ivStamp;
    private TextView tvTimes;

    private int remainTimes;

    private StampFullUtil stampUtil;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_stamp_count_limit);

        try {
            stampUtil = new StampFullUtil(this, this);
        } catch (Exception e) {
            e.printStackTrace();
            finish();
            return;
        }

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            stampUtil.setSealApplyId(bundle.getString("sealApplyId"));
            macAddress = bundle.getString("macAddress");
            stampUtil.setMacAddress(macAddress);
            sn = bundle.getString("equipSn");
            stampUtil.setSn(sn);
        }

        stampUtil.setLongitude(120.36D);
        stampUtil.setLatitude(36.12D);
        stampUtil.setLocationName("测试地址");

        if (BuildConfig.DEBUG) {
            ShowToast.shortTime(macAddress);
        }

        setTitleBarText("限次用印");

        initView(getWindow().getDecorView());
    }

    private void initView(View view) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        clickToBack();
                        return true;
                    }
                }
                return false;
            }
        });

        ivStamp = view.findViewById(R.id.iv_stamp_times_stamp);
        ivStamp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickToStamp();
            }
        });

        tvTimes = view.findViewById(R.id.tv_stamp_times_times);

        TextView tvExit = view.findViewById(R.id.tv_stamp_times_exit);
        tvExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickToBack();
            }
        });

        stampUtil.prepareStampEnv();
    }

    private void enableStampOperation() {
        ivStamp.setImageDrawable(
                ContextCompat.getDrawable(mContext, R.mipmap.prints));
        ivStamp.setClickable(true);
        tvTimes.setText(
                String.format(Locale.CHINA, "可申请授权: %d 次", remainTimes));
    }

    private void disableStampOperation() {
        ivStamp.setImageDrawable(ContextCompat.getDrawable(mContext, R.mipmap.prints1));
        ivStamp.setClickable(false);
    }

    private void clickToStamp() {
        stampUtil.stamp();
    }

    private void clickToBack() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (stampUtil != null) {
            stampUtil.release();
        }
        BluetoothLeService.disconnect();
    }

    @Override
    public void onRemainTimesCallback(int code, int times) {
        StampActivity.this.remainTimes = times;
        switch (code) {
            case 0:
                tvTimes.setText(
                        String.format(Locale.CHINA, "可申请授权: %d 次", remainTimes));
                break;
            case 1:
                tvTimes.setText(String.format(Locale.CHINA, "一次授权已下发\n可申请授权: %d 次",
                        remainTimes));
                break;
            default:
                tvTimes.setText(
                        String.format(Locale.CHINA, "可申请授权: %d 次", remainTimes));
                break;
        }
    }

    @Override
    public void onTimeLimitCallback(int code, String startTime, String endTime) {

    }

    @Override
    public void onSucessCallback(int code, String msg) {
        switch (code) {
            case StampUtil.StampCallback.ERR_STAMP_SUCCESS:
                enableStampOperation();
                break;
            default:
                break;
        }
    }

    @Override
    public void onErrorCallback(int code, String msg) {
        switch (code) {
            default:
                enableStampOperation();
                break;
        }
        if (code == StampUtil.StampCallback.ERR_STAMP_NONE_BLE_OPEN) {
            getMActivity().finish();
        }
    }

    @Override
    public void onProgressCallback(int code, String msg) {
        switch (code) {
            case StampUtil.StampCallback.ERR_STAMP_PREPARE_PROGRESS:
                disableStampOperation();
                break;
            case StampUtil.StampCallback.ERR_STAMP_ROLLBACK_PROGRESS:
            case StampUtil.StampCallback.ERR_STAMP_ROLLBACK_PROGRESS_IGNORE:
                enableStampOperation();
            default:
                break;
        }
    }
}
